package com.foreknow.controller;

import com.foreknow.model.Guestbook;
import com.foreknow.service.GuestbookService;
import com.foreknow.service.impl.GuestbookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ManagerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数q=list
        String method = req.getParameter("q");
        if("list".equals(method)) {
            list(req,resp);
        }else if("delete".equals(method)) {
            delete(req,resp);
        }else if("update".equals(method)) {
            update(req,resp);
        }else if("updateGuestbook".equals(method)) {
            updateGuestbook(req,resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置服务器端向客户端响应的内容的类型(MIME) https://www.runoob.com/http/mime-types.html
        resp.setContentType("text/html;charset=utf-8");
        //获取输出流主要用于向网页中显示信息
        PrintWriter out = resp.getWriter();
        //调用Service中的方法
        GuestbookService service = new GuestbookServiceImpl();
        int id = Integer.parseInt(req.getParameter("id")); //使用包装类将String转换为int类型
        service.deleteById(id);
        list(req,resp); //调用list方法重新查询

    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置服务器端向客户端响应的内容的类型(MIME) https://www.runoob.com/http/mime-types.html
        resp.setContentType("text/html;charset=utf-8");
        //获取输出流主要用于向网页中显示信息
        PrintWriter out = resp.getWriter();
        //调用Service中的方法(根据id查询留言信息)
        GuestbookService service = new GuestbookServiceImpl();
        int id = Integer.parseInt(req.getParameter("id")); //使用包装类将String转换为int类型
        Guestbook gb = service.getQueryById(id);
        req.setAttribute("gb",gb);
        req.getRequestDispatcher("/update.jsp").forward(req,resp);

    }

    protected void updateGuestbook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //修改
        //设置服务器端向客户端响应的内容的类型(MIME) https://www.runoob.com/http/mime-types.html
        resp.setContentType("text/html;charset=utf-8");
        //获取输出流主要用于向网页中显示信息
        PrintWriter out = resp.getWriter();
        String id = req.getParameter("id");
        //调用Service中的方法
        GuestbookService service = new GuestbookServiceImpl();
        Guestbook gb = new Guestbook();
        gb.setId(Integer.parseInt(id));
        gb.setName(req.getParameter("username"));
        gb.setPass(req.getParameter("password"));
        gb.setPhone(req.getParameter("phone"));
        gb.setEmail(req.getParameter("email"));
        gb.setTitle(req.getParameter("title"));
        gb.setContent(req.getParameter("content"));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        gb.setCreatedtime(simpleDateFormat.format(new Date()));
        service.updateInfo(gb);
        list(req,resp);
        //req.getRequestDispatcher("manager?q=list").forward(req,resp);
    }

    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置服务器端向客户端响应的内容的类型(MIME) https://www.runoob.com/http/mime-types.html
        resp.setContentType("text/html;charset=utf-8");
        //获取输出流主要用于向网页中显示信息
        PrintWriter out = resp.getWriter();
        HttpSession session = req.getSession();//获取session对象
        //调用Service中的方法
        GuestbookService service = new GuestbookServiceImpl();
        List<Guestbook> list = service.getAll();
        //要将以上的list集合在服务器端保存并传递给list.jsp
        session.setAttribute("list",list);
        //跳转资源
        resp.sendRedirect("/WebManagerSystem/list.jsp");
        //req.getRequestDispatcher("/list.jsp").forward(req,resp);

    }

    protected void jump(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //转发资源AddMessage.html
    }






}
